<?php
namespace app\walletyhbbadmin\controller;
use Think\Db;
use think\Collection;
use Think\page;
use think\Session;
class Goods extends AdminBase{
    //空操作
    public function _empty(){
        header("HTTP/1.0 404 Not Found");
        $this->display('Public:404');
    }

    /**
     * 轮播图列表
     */
    public function lists()
    {
        $list = DB::name('Chart')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 添加轮播图
     */
    public function add_lists(){
        $list = DB::name('Chart_type')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 添加轮播图方法
     */
    public function goods_add()
    {
        $data['c_sort'] = input('sort');
        $file = request()->file('c_pic');
        if(!empty($file)){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' );
            $data['c_pic'] = '/public/uploads/' . str_replace('\\', '/', $info->getsaveName());
        }
        if($data['c_pic'] == null)
        {
            $this->error('请完善信息');
        }
        $res = DB::name('Chart')->insert($data);
        if($res)
        {
            $this->success('添加成功','goods/lists');
        }else{
            $this->error('系统繁忙,请稍后再试');
        }
    }

    /**
     *   删除轮播图
     */
    public function deletes(){
        //接id
        $goods_id = input('goods_id');
        //删除数据库
        $bool = Db::name('Chart')->where('id',$goods_id)->delete();

        //判断是否删除成功
        if($bool){
            $this->success('删除成功','goods/lists');
        }else{
            $this->error('系统繁忙,请稍后再试');
        }
    }

    /**
     *   商品修改页面
     */
   public function edit(){
       //接id
       $goods_id=input('goods_id');
       //查数据库
       $info = DB::name('Chart')->where('id',$goods_id)->find();
       $this->assign('info',$info);
       return $this->fetch('goods/edit');
   }

    /*
     * 执行修改
     * */
    public function update(){
        $id = input('id');
        $list = DB::name('Chart')->where('id',$id)->find();
        $c_sort = $list['c_sort'];
        $data['c_sort'] = input('c_sort');
        if($data['c_sort'] == 'c_sort')
        {
            $data['c_sort'] = $list['c_sort'];
        }
        $file = request()->file('c_pic');
        if(!empty($file)){
            $info = $file->validate(['size'=>4596167,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads' );
            $data['c_pic'] = '/public/uploads/' . str_replace('\\', '/', $info->getsaveName());
        }else{
            $data['pic'] = $list['c_pic'];
        }
//        dump($data['c_pic']);die();

        $res = DB::name('Chart')->where('id',$id)->update($data);
        if($res)
        {
            $this->success('修改成功','Goods/lists');
        }else{
            $this->error('未修改信息','Goods/lists');
        }
    }


}